// pages/other/index.js
const app = getApp(),
    core = require('../../utils/core');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: 0,
		files: [],
		successFiles:[],
        popupUplodStatus:false
    },
    // 上传选择展示
    uploadShow(){
        this.setData({
            popupUplodStatus:!this.data.popupUplodStatus
        })
    },
    //上传图片
    chooseImage(){
      let page = this;
	  let len = page.data.files.length;
	  let files = page.data.files;
      wx.chooseMedia({
        count: 15 - len,
        mediaType: ['image'],
        sourceType: ['album'],
        maxDuration: 30,
        camera: 'back',
        success(res) {
          let tempFiles = res.tempFiles;
          for(let i=0;i<tempFiles.length;i++){
            console.log(tempFiles[i])
            core.uploadFile({
              filePath: tempFiles[i].tempFilePath,
              success(ret, response) {
                let file = {
                    name: '图片',
                    file_ext: page.fileExt(ret.data.url),
                    file: ret.data.url
				};
                files.push(file);
				page.setData({
					files: files
				});
				page.uploadShow();
              }
            });
          }
        }
      })
    },

    fileExt(file_url){
      return file_url.substring(file_url.lastIndexOf('.') + 1)
    },

    //上传文件
    chooseMeesageFile(){
      let page = this;
      let len = page.data.files.length;
      wx.chooseMessageFile({
        count: 15-len,
        type: 'file',
        extension: ['pdf','doc','docx'],
        success (res) {
          // tempFilePath可以作为 img 标签的 src 属性显示图片
           const tempFilePaths = res.tempFiles;
           for(let i=0;i<tempFilePaths.length;i++){
            core.uploadFile({
              filePath: tempFilePaths[i].path,
              success(ret, response) {
                let files = page.data.files;
                let file = {
                  name: tempFilePaths[i].name,
                  file_ext: page.fileExt(ret.data.url),
                  file: ret.data.url
                };
                files.push(file);
				page.setData({
					files: files
				});
				page.uploadShow();
              }
            });
          }
        }
      })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      let page = this;
      core.get({url:'/xilujob.resume/resume_files',data:{},success(ret, response) {
        page.setData({files: ret.data,successFiles:ret.data});
      }})
    },
    // 提交数据
    bindSubmit(event) {
        let that = this;
        var files = that.data.files;
        if (files.length == 0) {
            wx.showToast({
                title: '请上传简历附件',
                icon: "none",
                duration: 3000
            });
            return false;
        }
        core.post({url:'/xilujob.resume/add_files',data:{files:files},success(ret, response) {

            wx.showToast({
              title: '附件上传成功',
              icon: "none",
              duration: 1000
			});
			// this.setData({
			// 	successFiles: ret.data
			// })
			setTimeout(function(){
				wx.navigateBack({});
			},1000)
			
        }});
    },
    // 案例删除图片
    clearImg: function (e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        let files = that.data.files;
        files.splice(index, 1);
        that.setData({
          files: files
        })
    },
    // 查看图片
    img_preview(e) {
        app.img_preview(e);
    },
    // 下载 文件
    downloadFile(e) {
        let url = e.currentTarget.dataset.src;
        app.downloadFile(url);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
		this.getOpenerEventChannel().emit('dataTransfer', {files: this.data.successFiles});
    }
})